package com.googlecode.wmlynar.gwtbeans.client.ui;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.wmlynar.gwtbeans.client.beans.SourcesPropertyChangeEvents;

/**
 * Wrapper for TextBox class that posts property change events.
 * 
 * @author Wojciech Mlynarczyk (wmlynar@gmail.com)
 * 
 */
public class CheckBox extends com.google.gwt.user.client.ui.CheckBox implements
        SourcesPropertyChangeEvents {

    PropertyChangeSupport support = new PropertyChangeSupport(this);

    public CheckBox() {
        super();
        addClickListener(new ClickListener() {
            public void onClick(final Widget arg0) {
                support.firePropertyChange("checked", null, isChecked());
            }
        });
    }

    public void setChecked(final boolean checked) {
        final boolean old = isChecked();
        super.setChecked(checked);
        support.firePropertyChange("checked", old, isChecked());
    }

    public void addPropertyChangeListener(final String propertyName,
            final PropertyChangeListener listener) {
        support.addPropertyChangeListener(propertyName, listener);
    }

    public void removePropertyChangeListener(final String propertyName,
            final PropertyChangeListener listener) {
        support.removePropertyChangeListener(propertyName, listener);
    }
}
